// pages/mall/goodsSpec/index.js
import { calcuActive, getFormList } from '../../../../utils/hooks';
import { debounce } from '../../../../utils/util';
const $api = require('../../../../request/api').API;
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsId: '',
    goodsInfo: {},
    keyword: '',
    filterList: [],
    list: [],
    noData: false,
    loading: false,
    formData: {
      val: {},
      show: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad({ goodsId }) {
    this.setData({
      goodsId
    });
    wx.showLoading({
      title: '数据加载中'
    });
    this.getGoodsInfo();
    try {
      await this.getSpec();
    } finally {
      wx.hideLoading();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  /**
   * 过滤列表
   */
  handleSearch: debounce(function (e) {
    let { list, filterList } = this.data;
    filterList = list.filter(item => item.sku.includes(e.detail) || item.introduce.includes(e.detail));
    this.setData({
      filterList,
      noData: filterList.length ? false : true
    });
  }, 200),
  // 获取产品信息
  async getGoodsInfo() {
    let res = await $api.getGoodsInfo({ id: this.data.goodsId });
    res = res.data.data;
    if (res.attrMark) {
      this.getFormInfo(res);
    }
    this.setData({
      goodsInfo: res
    });
  },
  // 获取商品不同规格
  async getSpec() {
    let res = await $api.getSpecByGoodsId({ goodsId: this.data.goodsId });
    res = res.data.data;
    const list = Object.values(res.variantMap);
    // 判断活动
    calcuActive(list);
    this.setData({
      list,
      filterList: list
    });
  },
  // 获取表单信息
  async getFormInfo(goodsInfo) {
    const res = await getFormList(goodsInfo);
    this.setData({
      'formData.val': res
    });
  },
  // 计算活动折扣时间
  // calcuActive(list){
  //   list.forEach(item=>{
  //     const {active,price} = item
  //     if(!active||!isActiveTime(item)) return
  //     item.activeDiscount = parseFloat(((active.activePrice / price) * 10).toFixed(2)) || 0.01
  //     item.activeNums =active.maxNum
  //     item.salePrice = active.activePrice
  //   })
  //   // this.setData({
  //   //   filterList:list
  //   // })

  // },
  // 获取表单数据
  getFormData(e) {
    console.log(e);
    this.tempFormData = e.detail.formData;
    // 获取到表单数据后直接去结算
    this.toSubmitOrder();
  },
  // 预览图片
  previewImg(e) {
    const { moreImg, mainImg } = e.currentTarget.dataset.item;
    let urls = [mainImg];
    if (moreImg) urls = urls.concat(moreImg.split(','));
    wx.previewImage({
      urls,
      current: urls[0]
    });
  },
  /**
   * 检查是否可以提交
   */
  checkSubmit(e) {
    this.item = e.currentTarget.dataset.item;
    if (this.data.goodsInfo.attrMark) {
      // 判断是否需要显示前置表单
      this.setData({
        'formData.show': true
      });
      return;
    }
    this.toSubmitOrder();
  },
  /**
   * 去提交订单
   */
  toSubmitOrder() {
    const mergeParams = {
      mainImage: this.item.mainImg,
      specVal: this.item.sku,
      nums: 1
    };
    app.globalData.goodsList = [Object.assign({ ...this.data.goodsInfo }, this.item, mergeParams)];
    app.globalData.tempFormList = [this.tempFormData];
    wx.navigateTo({
      url: '/subcontract/home/pages/submitOrder/index'
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
